/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.tmessage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.tmessage.BusinessMessageTemplate;
import com.boxin.ims.modules.wechat.entity.tmessage.UserMessageTemplate;
import com.boxin.ims.modules.wechat.service.tmessage.BusinessMessageTemplateService;
import com.boxin.ims.modules.wechat.service.tmessage.UserMessageTemplateService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用户消息模板Controller
 * @author 肖乾
 * @version 2014-09-11
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/tmessage/userMessageTemplate")
public class UserMessageTemplateController extends BaseController {

	@Autowired
	private UserMessageTemplateService userMessageTemplateService;
	
	@Autowired
	private BusinessMessageTemplateService businessMessageTemplateService;
	
	@ModelAttribute
	public UserMessageTemplate get(@RequestParam(required=false) Long id) {
		if (id != null){
			return userMessageTemplateService.get(id);
		}else{
			return new UserMessageTemplate();
		}
	}
	
	@RequiresPermissions("wechat:tmessage:userMessageTemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserMessageTemplate userMessageTemplate, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		User user = UserUtils.getUser();
		if(!user.getInterfaceFlag()){
			addMessage(redirectAttributes, "您没有开通高级接口！！");
			return "modules/wechat/tmessage/userMessageTemplateList";
		}
		if (!user.isAdmin()){
			userMessageTemplate.setUser(user);
		}
        Page<UserMessageTemplate> page = userMessageTemplateService.find(new Page<UserMessageTemplate>(request, response), userMessageTemplate); 
        model.addAttribute("page", page);
		return "modules/wechat/tmessage/userMessageTemplateList";
	}

	@RequiresPermissions("wechat:tmessage:userMessageTemplate:view")
	@RequestMapping(value = "form")
	public String form(UserMessageTemplate userMessageTemplate, Model model) {
	
		User user = UserUtils.getUser();
		if (userMessageTemplate.getUser() == null || userMessageTemplate.getUser().getId() <1){
			userMessageTemplate.setUser(user);
		}
		
		model.addAttribute("userMessageTemplate", userMessageTemplate);
		return "modules/wechat/tmessage/userMessageTemplateForm";
	}

	@RequiresPermissions("wechat:tmessage:userMessageTemplate:edit")
	@RequestMapping(value = "save")
	public String save(UserMessageTemplate userMessageTemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userMessageTemplate)){
			return form(userMessageTemplate, model);
		}
		User user = UserUtils.getUser();
		if(!user.getInterfaceFlag()){
			addMessage(redirectAttributes, "您没有开通高级接口！！");
			return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/userMessageTemplate/?repage";
		}
		
		Long count=  userMessageTemplateService.getUserMessageTemplateCountByBid(userMessageTemplate);
		if(count!=0){
			addMessage(redirectAttributes, "一个模板只能选择一个");
			return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/userMessageTemplate/form?repage";
		}
		BusinessMessageTemplate businessMessageTemplate=  businessMessageTemplateService.get(userMessageTemplate.getBusinessMessageTemplate().getId());
		userMessageTemplate.setName(businessMessageTemplate.getName());
		userMessageTemplateService.save(userMessageTemplate);
		addMessage(redirectAttributes, "保存用户消息模板'" + userMessageTemplate.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/userMessageTemplate/?repage";
	}
	
	@RequiresPermissions("wechat:tmessage:userMessageTemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		userMessageTemplateService.delete(id);
		addMessage(redirectAttributes, "删除用户消息模板成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/userMessageTemplate/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "treeData")
	public String treeData(@RequestParam(required=false) Long extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		
		BusinessMessageTemplate businessMessageTemplate=new BusinessMessageTemplate();
		
		List<BusinessMessageTemplate> messageTemplateList=  businessMessageTemplateService.find(businessMessageTemplate);
		
			for (int i=0; i<messageTemplateList.size(); i++){
				BusinessMessageTemplate e = messageTemplateList.get(i);
					Map<String, Object> map = Maps.newHashMap();
					map.put("id", e.getId());
					map.put("pId", e.getId());
					map.put("name", e.getName());
					mapList.add(map);
				}
		
		return JsonMapper.getInstance().toJson(mapList);
	}
	
	

}
